//
//  OpinionViewController.m
//  TableViewGroup
//
//  Created by 侯振兴 on 15/1/6.
//  Copyright (c) 2015年 侯振兴. All rights reserved.
//

#define SCREEN_WIDTH ([UIScreen mainScreen].bounds.size.width)
#define SCREEN_HEIGHT ([UIScreen mainScreen].bounds.size.height)
#import "OpinionViewController.h"
#import "SKPSMTPMessage.h"
#import "NSData+Base64Additions.h"

@interface OpinionViewController ()<UIAlertViewDelegate>

@end

@implementation OpinionViewController

- (void)dealloc
{
    [_field1 release];
    [_field release];
    [_bAlert release];
    [super dealloc];
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.view.backgroundColor = [UIColor whiteColor];
    self.navigationController.navigationBar.translucent = NO;
    UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake(0, 150, SCREEN_WIDTH, SCREEN_HEIGHT - 264)];
    image.image = [UIImage imageNamed:@"wolfgang3.jpg"];;
    [self.view addSubview:image];
    self.navigationItem.title = @"意见反馈";
    [self createOpinion];
}


- (void)createOpinion
{
    self.field = [[UITextField alloc] initWithFrame:CGRectMake(0, 0, SCREEN_WIDTH, 150)];
//    _field.layer.cornerRadius = 20;
//    _field.clipsToBounds = YES;
    _field.borderStyle = UITextBorderStyleRoundedRect;
    _field.placeholder = @"请输入反馈信息";
    
    self.field1 = [[UITextField alloc] initWithFrame:CGRectMake(20, 160, SCREEN_WIDTH - 40, 30)];
    _field1.borderStyle = UITextBorderStyleRoundedRect;
    _field1.placeholder = @"请输入您的邮箱";

    UIButton *button = [[UIButton alloc] initWithFrame:CGRectMake(SCREEN_WIDTH / 2, 10, SCREEN_WIDTH / 7, 30)];
    [button setBackgroundColor:[UIColor clearColor]];
    [button setTitle:@"发送" forState:UIControlStateNormal];
    [button setTitleColor:[UIColor blueColor] forState:UIControlStateNormal];
    [button addTarget:self action:@selector(buttonAction:) forControlEvents:UIControlEventTouchUpInside];
    button.titleLabel.alpha = 0.5;
    self.navigationItem.rightBarButtonItem = [[[UIBarButtonItem alloc] initWithCustomView:button] autorelease];
    
    [self.view addSubview:_field];
    [self.view addSubview:_field1];
    [self.view addSubview:button];
    [_field1 release];
    [_field release];
    [button release];
}

- (void)buttonAction:(id)sender
{
    if ([_field.text isEqualToString:@""]) {
        self.bAlert = [[UIAlertView alloc] initWithTitle:@"信息不能为空的" message:nil delegate:self cancelButtonTitle:nil otherButtonTitles:nil, nil];
        [_bAlert show];
        [NSTimer scheduledTimerWithTimeInterval:2.0f target:self selector:@selector(timerAction:) userInfo:nil repeats:NO];
        [_bAlert release];
        
    } else {
    UIAlertView *aAlert = [[UIAlertView alloc] initWithTitle:@"嘟..嘟..嘟.." message:@"是否发送" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
    [aAlert show];
    [aAlert release];
    }
}

-(void)alertView:(UIAlertView *)alertView didDismissWithButtonIndex:(NSInteger)buttonIndex
{
    if (buttonIndex == 0) {
        SKPSMTPMessage *testMsg = [[SKPSMTPMessage alloc] init];
        testMsg.fromEmail = @"wowchina1010@126.com";
        testMsg.toEmail = @"1226423229@qq.com";
        testMsg.relayHost = @"smtp.126.com";
        testMsg.requiresAuth = YES;
        if (testMsg.requiresAuth) {
            testMsg.login = @"wowchina1010@126.com";
            testMsg.pass = @"19871010";
        }
        
        testMsg.wantsSecure = YES;
        testMsg.subject = @"反馈意见";
        testMsg.delegate = self;
        NSString *str = [_field.text stringByAppendingString:_field1.text];
        NSDictionary *plainPart = [NSDictionary dictionaryWithObjectsAndKeys:@"text/plain; charset=UTF-8",kSKPSMTPPartContentTypeKey,
                                   str,kSKPSMTPPartMessageKey,@"8bit",kSKPSMTPPartContentTransferEncodingKey,nil];
        testMsg.parts = [NSArray arrayWithObjects:plainPart,nil];
        dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^{
            [testMsg send];
        });

    }
}


- (void)timerAction:(id)sender
{
    [_bAlert dismissWithClickedButtonIndex:0 animated:YES];
}

- (void)messageSent:(SKPSMTPMessage *)message
{
    
}

- (void)messageFailed:(SKPSMTPMessage *)message error:(NSError *)error
{
    [message release];
}

-(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event
{
    [self.view endEditing:YES];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
